//
//  XPhotoCollectionCell.m
//  XPhotoLibraryKit_Example
//
//  Created by Wu,Xinting on 2018/3/20.
//  Copyright © 2018年 wuxinting. All rights reserved.
//

#import "XPhotoCollectionCell.h"
#import "UIImage+Resource.h"
#import "XPhoto.h"

@interface XPhotoCollectionCell ()

@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UIImageView *playImageView;
@property (nonatomic, strong) UIButton *checkButton;

@end

@implementation XPhotoCollectionCell

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.imageView.frame = self.bounds;
        [self.contentView addSubview:self.imageView];
        
        self.playImageView.hidden = YES;
        [self.imageView addSubview:self.playImageView];
        
        self.checkButton.hidden = YES;
        [self.checkButton addTarget:self action:@selector(clickCheck:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:self.checkButton];
    }
    return self;
}

- (void)refresh {
    if (self.photo.functionalImage) {
        self.imageView.image = self.photo.functionalImage;
        return;
    }
    
    PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
    options.synchronous = NO;
    options.networkAccessAllowed = YES;
    CGSize tSize = CGSizeMake(self.imageView.bounds.size.width * 2, self.imageView.bounds.size.height * 2);
    [[PHImageManager defaultManager] requestImageForAsset:self.photo.asset targetSize:tSize contentMode:PHImageContentModeDefault options:options resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
        self.imageView.image = result;
    }];
    if (self.photo.asset.mediaType == PHAssetMediaTypeVideo) {
        self.playImageView.center = self.imageView.center;
        self.playImageView.hidden = NO;
    } else {
        self.playImageView.hidden = YES;
    }
}

- (void)prepareForReuse {
    [super prepareForReuse];
    self.checkButton.hidden = YES;
}

#pragma mark - Action
- (void)clickCheck:(UIButton *)btn {
    btn.selected = !btn.isSelected;
    self.photo.checked = btn.selected;
}

#pragma mark - Get&Set
- (void)setEditable:(BOOL)editable {
    _editable = editable;
    
    // 功能性的图标
    if (self.photo.functionalImage) return;
    
    self.checkButton.selected = self.photo.checked;
    self.checkButton.hidden = !self.editable;
}

- (UIImageView *)imageView {
    if (!_imageView) {
        _imageView = [[UIImageView alloc] initWithFrame:CGRectZero];
        _imageView.clipsToBounds = YES;
        _imageView.contentMode = UIViewContentModeScaleAspectFill;
    }
    return _imageView;
}

- (UIImageView *)playImageView {
    if (!_playImageView) {
        CGFloat size = self.imageView.bounds.size.width / 2;
        _playImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, size, size)];
        _playImageView.image = [UIImage imageForName:@"play"];
    }
    return _playImageView;
}

- (UIButton *)checkButton {
    if (!_checkButton) {
        _checkButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
        [_checkButton setImage:[UIImage imageForName:@"unchecked"] forState:UIControlStateNormal];
        [_checkButton setImage:[UIImage imageForName:@"checked"] forState:UIControlStateSelected];
    }
    return _checkButton;
}

@end
